//
//  GameViewController.m
//  momo
//
//  Created by HappyMilk 02 on 2017/5/1.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "GameViewController.h"
#import "GameScene.h"
#import "HomeViewController.h"
#import "MessageModel.h"
#import "LoginHeader.h"

@implementation GameViewController{
    
    SKView *_skview;
    NSMutableArray *msgArr;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _skview.paused = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _skview.paused = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [MessageModel registerSubclass];
    msgArr = [NSMutableArray array];
    // Load the SKScene from 'GameScene.sks'
    GameScene *scene = [GameScene sceneWithSize:self.view.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    _skview = (SKView *)self.view;
    [_skview presentScene:scene];
    
    //_skview.showsFPS = YES;
    //_skview.showsNodeCount = YES;
    
    [self getAllMessage];
    
    scene.tapNodeAction = ^(NSString *nodeName) {
        if ([nodeName isEqualToString:@"mine"]) {
            
            if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
                return;
            }
            
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"MineViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([nodeName isEqualToString:@"send"]) {
            
            if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
                return;
            }
            
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"SendViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([nodeName isEqualToString:@"plan"]) {
            
            if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
                return;
            }
            
            int a = arc4random()%100;
            NSLog(@"点击 ---- %d",a);
            if (a>70) {
                
                MessageModel *model = msgArr[arc4random()%msgArr.count];
                UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                HomeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"HomeViewController"];
                vc.msg_user_id = model.user_id;
                vc.message = model.message;
                vc.msg_id = model.objectId;
                vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                [self presentViewController:vc animated:YES completion:nil];
            }else{
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hint" message:@"Nothing,please touch other plans" delegate:self cancelButtonTitle:@"Confirm" otherButtonTitles:nil, nil];
                [alert show];
            }
            
            
        }
        
    };
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hint" message:@"Please touch these plans,repeat,surprise" delegate:self cancelButtonTitle:@"Confirm" otherButtonTitles:nil, nil];
    [alert show];
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (void)getAllMessage{

    AVQuery *query = [AVQuery queryWithClassName:@"Msg_list"];
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (objects.count>0) {
            msgArr = [objects mutableCopy];
        }
    }];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

@end
